//
//  KKIntroductionView.m
//  DoMi
//
//  Created by 康凯 on 2016/11/9.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKIntroductionView.h"
#import "KKVideoDetail.h"
#import "KKIntroContentCell.h"
#import "KKRelateCell.h"
#import "KKVideo1Cell.h"
#import "KKVideoDetailController.h"
#import "KKArticleDetailM.h"
#import "KKArticleListM.h"
#import "KKArticleDetailVC.h"

@interface KKIntroductionView()<UITableViewDelegate,UITableViewDataSource>

@property(strong, nonatomic)NSArray *cellId;

@property(strong,nonatomic)KKVideoDetail *detailData;
@property(strong,nonatomic)KKArticleDetailM *articleData;
@property(strong,nonatomic)NSArray *items;
@property(strong,nonatomic)NSArray *listData;
@property(copy,nonatomic)NSString *flag;
@property(assign,nonatomic)int moreFlag;
@property(assign,nonatomic)float contentHeight;
@end
@implementation KKIntroductionView

-(void)layoutSubviews{
    self.frame = CGRectMake(0, 0, screenWidth, self.height);
}

-(void)initView{
    self.moreFlag=0;
    self.introTableView.delegate=self;
    self.introTableView.dataSource=self;
    self.cellId=@[@"KKIntroContentCell",@"KKRelateCell",@"KKVideo1Cell"];
    for (NSString *cell in self.cellId) {
        [self.introTableView registerNib:[UINib nibWithNibName:cell bundle:nil] forCellReuseIdentifier:cell];
    }
}

#pragma mark 更新数据
-(void)refreshData:(int)section setData:(id)data{
    switch (section) {
        case 0:
            self.detailData=data;
            break;
        case 1:
            self.items=data;
            break;
        case 2:
            self.listData=data;
            break;
    }
    self.flag=@"0";
    [self.introTableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
}
#pragma mark 文章详情更新数据
-(void)refreshArticleData:(int)section setData:(id)data{
    switch (section) {
        case 0:
            self.articleData=data;
            break;
        case 1:
            self.items=data;
            break;
        case 2:
            self.listData=data;
            break;
    }
    self.flag=@"1";
    [self.introTableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark tableview delegate

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        KKIntroContentCell *content=(KKIntroContentCell *)cell;
        content.moreClickBlock=^(int flag){
            self.moreFlag=flag;
            self.contentHeight=[KKAppUtils sizeWithText:self.detailData.videodesc font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(screenWidth-55, MAXFLOAT)].height;
            [self.introTableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
            [self.introTableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
        };
        if([self.flag isEqualToString:@"0"]){
            [content initData:self.detailData];
        }else{
            [content initArticleData:self.articleData];
        }
        
    }else if(indexPath.section==1){
        KKRelateCell *item=(KKRelateCell *)cell;
        self.relateCell=item;
        if([self.flag isEqualToString:@"0"]){
            [item initData:self.items relateTitle:@"相关课程"];
        }else{
            [item initData:self.items relateTitle:@"相关文章"];
        }
    }else{
        KKVideo1Cell *list=(KKVideo1Cell *)cell;
        list.backgroundColor=[UIColor whiteColor];
        if([self.flag isEqualToString:@"0"]){
            [list initData:self.listData[indexPath.row]];
        }else{
            [list initArticleData:self.listData[indexPath.row]];
        }
        
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==2){
        return self.listData.count;
    }else{
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:self.cellId[indexPath.section]];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        if(self.moreFlag==0){
            return 140;
        }else{
            return 100+self.contentHeight;
        }
    }else if(indexPath.section==1){
        return 49;
    }else{
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.frame.size.height;
    }
    return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if(indexPath.section==2){
        if([self.flag isEqualToString:@"0"]){
            KKVideoDetailController *detail=[[KKVideoDetailController alloc]init];
            KKStudyList *list= self.listData[indexPath.row];
            detail.videoId=list.videoid;
            [[KKAppUtils getCurrentVisiableVC:self].navigationController pushViewController:detail animated:YES];
        }else{
            KKArticleListM *data= self.listData[indexPath.row];
            KKArticleDetailVC *detail=[[KKArticleDetailVC alloc] init];
            detail.docId=data.articleid;
            [[KKAppUtils getCurrentVisiableVC:self].navigationController pushViewController:detail animated:YES];
        }
        
    }
}

@end
